/**
 * 功能描述 联盟团信息导航条
 */

define(function (require, exports, module) {
	var EditGrid = require("editgrid");
	var store =require("store");
	var activeUser = store.get("user");
	
	var editGridEvents = require("../handler/editGridEvents");
	
    var instance = {
        initComponent: function (params, widget) {
            var editgrid = new EditGrid({
            	parentNode : ".J-editgrid",
            	model : {
                    id : "memberRegisterEditGrid",
                    onAdd: function(editors) {
                        var idNumber = editors["personalInfo.idNumber"];
                        var name = editors["personalInfo.name"];
                        var sex = editors["personalInfo.sex"];
                        var mobilePhone = editors["personalInfo.mobilePhone"];
                        var contactName = editors["contactName"];
                        var contactPhone = editors["contactPhone"];
                        
                        if(idNumber.getValue() == "" || name.getValue() == ""
                        	|| sex.getData(sex.getValue()).length == 2
                        	|| mobilePhone.getValue() == ""){
                        	return false;
                        }
                        
                        if(!editGridEvents.checkData(editors, widget)){
                        	return false;
                        }
                        	
                        editgrid.add({
                        	personalInfo : {
                        		idNumber: idNumber.getValue(),
                        		name: name.getValue(),
                        		sex: sex.getData(sex.getValue()).key,
                        		mobilePhone: mobilePhone.getValue(),
                        		version : 0,
                        	},
                        	contactName : contactName.getValue(),
                        	contactPhone : contactPhone.getValue(),
                        	signUpDate : moment().format("YYYY-MM-DD"),
                        	member : false,
                        	version : 0
                        });
                        
                        editGridEvents.countPerNumber(widget);
                    },
                    columns : [{
                        name : "personalInfo.idNumber",
                        label : "身份证号",
                        editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                            	if(!rowData.personalInfo){
                            		rowData.personalInfo = {};
                            	}
                                rowData.personalInfo.idNumber = editor.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }
                    },{
                        name : "personalInfo.name",
                        label : "姓名",
                        editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                            	if(!rowData.personalInfo){
                            		rowData.personalInfo = {};
                            	}
                                rowData.personalInfo.name = editor.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }
                    },{
                    	name : "personalInfo.sex",
                        label : "性别",
                        format:function(value,row){
                        	if(typeof value == "string"){
                        		if(value == "Male"){
                        			return "男";
                        		}else{
                        			return "女";
                        		}
                        	}else{
                        		if(value.key == "Male"){
                        			return "男";
                        		}else{
                        			return "女";
                        		}
                        	}
                        },
                        editor: {
                            type : "select",
                            options : [{
                            	key : "Male",
                            	value : "男"
                            },{
                            	key : "Female",
                            	value : "女"
                            }],
                            onChange: function(editor, rowIndex, rowData) {
                            	if(!rowData.personalInfo){
                            		rowData.personalInfo = {};
                            	}
                                rowData.personalInfo.sex = editor.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }
                    },{
                    	name : "personalInfo.mobilePhone",
                    	label : "手机号",
                    	editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                            	if(!rowData.personalInfo){
                            		rowData.personalInfo = {};
                            	}
                                rowData.personalInfo.mobilePhone = editor.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }
                    },{
                    	name : "contactName",
                    	label : "紧急联系人",
                    	editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                                rowData.contactName = editor.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }  
                    },{
                    	name : "contactPhone",
                    	label : "紧急联系电话",
                    	editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                                rowData.contactPhone = editor.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }  
                    },{
                    	name : "signUpDate",
                    	label : "报名日期",
                    	format :"date",
   					 	formatparams:{
   							mode:"YYYY-MM-DD"
   					 	}
                    },{
                    	 name : "operate",
    					 label : "操作",
    					 format : "button",
						 formatparams:[{
							id : "remove",
							icon:"icon-remove",
							handler:function(index,data,rowEle){
								if(data.pkSignUpRegister){
									widget.get("pkSignList").push(data.pkSignUpRegister);
								}
								editgrid.remove(rowEle);
							}
						 }]
                    }]
                }
            })
        	widget.set("editgrid", editgrid);
        }
    };
    module.exports = instance;
});
